/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dew.cobranzas.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dew.cobranzas.model.Contrato;
import pe.edu.upc.dew.cobranzas.model.Inquilino;
import pe.edu.upc.dew.cobranzas.model.Propiedad;
import pe.edu.upc.dew.cobranzas.service.InquilinoServiceImpl;
import pe.edu.upc.dew.cobranzas.service.PropiedadServiceImpl;

/**
 *
 * @author Ivan
 */
public class ContratoDaoImpl implements ContratoDao {

    private InquilinoServiceImpl inquilinoService;
    private PropiedadServiceImpl propiedadService;

    public void setInquilinoService(InquilinoServiceImpl inquilinoService) {
        this.inquilinoService = inquilinoService;
    }

    public void setPropiedadService(PropiedadServiceImpl propiedadService) {
        this.propiedadService = propiedadService;
    }
    public List<Contrato> contratos = new ArrayList<Contrato>();

    public int getNuevoCodido() {
        return contratos.size() + 1;
    }

    public boolean Registra(int idContrato, Inquilino inquilino, Propiedad propiedad, String fechaContrato, String fechaVencimiento, double monto, String moneda, double moraDiaria) {


        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("insert into T_Contrato values (" + idContrato + ",'" + fechaContrato + "','" + fechaVencimiento + "'," + monto + ",'" + moneda + "'," + moraDiaria + ",'" + inquilino.getDni() + "'," + propiedad.getIdPropiedad() + ")");
        } catch (Exception e) {
            e.printStackTrace();
             return false;
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
             return true;
        }

       
    }

    public boolean Elimina(Contrato contrato) {
        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("delete from t_contrato where idContrato=" + contrato.getIdContrato() + "");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }


    }

    public Contrato getContratoPorCodigo(Integer idContrato) {
        Contrato contrato = new Contrato();
        for (int i = 1; i <= this.contratos.size(); i++) {
            if (this.contratos.get(i - 1).getIdContrato() == idContrato) {
                contrato = this.contratos.get(i - 1);
                break;
            }
        }

        return contrato;
    }

    public List<Contrato> Listado() {
        contratos.clear();
        Connection connection = null;
        Contrato contrato = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select * from T_Contrato");
            rs.next();
            do {

                contrato = new Contrato();
                contrato.setIdContrato(rs.getInt("idContrato"));
                contrato.setFechaContrato(rs.getString("fechaContrato"));
                contrato.setFechaVencimiento(rs.getString("fechaVencimiento"));
                contrato.setMonto(rs.getDouble("monto"));
                contrato.setMoneda(rs.getString("moneda"));
                contrato.setMoraDiaria(rs.getDouble("moraDiaria"));
                contrato.setInquilino(inquilinoService.getInquilinoPorDni(rs.getString("dni")));
                contrato.setPropiedad(propiedadService.getPropiedadPorId(rs.getInt("idPropiedad")));
                this.contratos.add(contrato);

            } while (rs.next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return contratos;
    }
}
